package edu.scau.mis.rest.service.impl;

import edu.scau.mis.core.redis.RedisCache;
import edu.scau.mis.rest.entity.RestItem;
import edu.scau.mis.rest.entity.vo.RestItemVoForCombo;
import edu.scau.mis.rest.mapper.RestItemMapper;
import edu.scau.mis.rest.service.RestItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * (RestItem)表服务实现类
 *
 * @author makejava
 * @since 2022-03-10 00:37:47
 */
@Service("restItemService")
public class RestItemServiceImpl implements RestItemService {
    @Resource
    private RestItemMapper restItemMapper;

    @Autowired
    RedisCache redisCache;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public RestItem queryById(Long id) {
        return this.restItemMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param restItem 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<RestItem> queryByPage(RestItem restItem, PageRequest pageRequest) {
        long total = this.restItemMapper.count(restItem);
        return new PageImpl<>(this.restItemMapper.queryAllByLimit(restItem, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param restItem 实例对象
     * @return 实例对象
     */
    @Override
    public RestItem insert(RestItem restItem) {
        this.restItemMapper.insert(restItem);
        return restItem;
    }

    /**
     * 修改数据
     *
     * @param restItem 实例对象
     * @return 实例对象
     */
    @Override
    public RestItem update(RestItem restItem) {
        this.restItemMapper.update(restItem);
        return this.queryById(restItem.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.restItemMapper.deleteById(id) > 0;
    }

    /**
     * 餐品加入购物车
     *
     * @param itemNo
     * @param method
     * @param openid
     * @return 当前产品总数
     */
    @Override
    public BigDecimal processItem(String itemNo, String method, String openid){
        BigDecimal totalAmount = BigDecimal.valueOf(0);

        //标识用户的餐位，uuid作为key
        String seatNo = (redisCache.getListByIndex(openid, 0)).toString();
        //查询该产品
        RestItem restItem = restItemMapper.queryItemByItemNo(itemNo);

        //列表不存在产品，请求添加产品时，直接添加
        if(redisCache.getListLength(seatNo) == 0 && method.equals("add")){
            restItem.setSaleAmount(restItem.getPrice());
            redisCache.addList(seatNo, restItem);
            //缓存进redis
            redisCache.setListByIndex(openid, 2, totalAmount);
            return restItem.getPrice(); //只有1个产品
        }

        int flag = 0; //中间变量，判断列表中是否存在该产品
        for(long i = 0; i < redisCache.getListLength(seatNo); i++){

            //列表中存在该产品时，处理产品数量
            if(((RestItem)redisCache.getListByIndex(seatNo, i)).getItemNo().equals(restItem.getItemNo())){
                flag = 1; //存在该产品，中间变量设置为1
                int num = ((RestItem)redisCache.getListByIndex(seatNo, i)).getNum();
                if(method.equals("add")) {
                    //数量 + 1
                    restItem.setNum(num + 1);
                    //更新单品总价
                    restItem.setSaleAmount(restItem.getPrice().multiply(BigDecimal.valueOf(num + 1)));
                    //更新结点
                    redisCache.setListByIndex(seatNo, i, restItem);
                }
                if(method.equals("reduce")) {
                    //数量 - 1
                    restItem.setNum(num - 1);
                    //更新单品总价
                    restItem.setSaleAmount(restItem.getPrice().multiply(BigDecimal.valueOf(num + 1)));
                    //更新结点
                    redisCache.setListByIndex(seatNo, i, restItem);

                    //该产品数量为0，删除该节点
                    if(((RestItem)redisCache.getListByIndex(seatNo, i)).getNum() == 0){
                        redisCache.delList(seatNo, i, restItem);
                    }
                    //列表为空，删除该key
                    if(redisCache.getListLength(seatNo) == 0){
                        redisCache.deleteObject(seatNo);
                        return BigDecimal.valueOf(0);
                    }
                }
            }

        }

        //列表不存在该产品时，添加该产品
        if(flag == 0){
            restItem.setSaleAmount(restItem.getPrice());
            redisCache.addList(seatNo, restItem);
        }

        //计算总金额
        for(long i = 0; i < redisCache.getListLength(seatNo); i++){
            totalAmount = totalAmount.add(((RestItem)redisCache.getListByIndex(seatNo, i)).getSaleAmount());
        }

        redisCache.setListByIndex(openid, 2, totalAmount);
        return totalAmount;
    }

    /**
     * 通过产品证号查询产品
     *
     * @param itemNo
     * @return
     */
    @Override
    public RestItem queryItemByItemNo(String itemNo){
        return this.restItemMapper.queryItemByItemNo(itemNo);
    }

    /**
     * 通过类别证号查询套餐
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<RestItemVoForCombo> queryComboByCategoryId(long categoryId){return this.restItemMapper.queryComboByCategoryId(categoryId);}
}
